package com.hk.dao.impl;

import com.hk.dao.ICompanyMessageDao;
import com.hk.model.CompanyMessage;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: zhe
 * @Date: 2021/6/11 15:12
 * @Statement: 公司简介Service层
 */
public class CompanyMessageDaoImpl implements ICompanyMessageDao {

    //    JDBC连接工具类
    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();
    private static String sql = "";

    @Override
    public ResultSet findAll(Integer page, Integer limit, CompanyMessage t) {
        sql = "select * from company_message where 1=1 ";
        if (!t.getStartTime().equals("")) {
            sql += " and start_time >= "+ SqlSymbol.MARK.getValue()+t.getStartTime()+SqlSymbol.MARK.getValue();
        }
        if (!t.getEndTime().equals("")) {
            sql += " and end_time <= "+SqlSymbol.MARK.getValue()+t.getEndTime()+SqlSymbol.MARK.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit, CompanyMessage t) {
        sql = "select count(1) from company_message where 1=1 ";
        if (!t.getStartTime().equals("")) {
            sql += " and start_time >= "+ SqlSymbol.MARK.getValue()+t.getStartTime()+SqlSymbol.MARK.getValue();
        }
        if (!t.getEndTime().equals("")) {
            sql += " and end_time <= "+SqlSymbol.MARK.getValue()+t.getEndTime()+SqlSymbol.MARK.getValue();
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from company_message where id = "+id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(CompanyMessage t) {
        sql = "insert into company_message value " +
                "("+null+ SqlSymbol.COMMA.getValue()+SqlSymbol.MARK.getValue()+t.getName()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getNameEnglish()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getPhone()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getStartTime()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getEndTime()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getIdea()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getIdeaEnglish()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getProfile()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getTitle()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getDetail()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                t.getState()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                SqlSymbol.RBRACKET.getValue();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int update(CompanyMessage t) {
        sql = "update company_message set " +
                "name = "+SqlSymbol.MARK.getValue()+t.getName()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "name_english = "+SqlSymbol.MARK.getValue()+t.getNameEnglish()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "phone = "+SqlSymbol.MARK.getValue()+t.getPhone()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "start_time = "+SqlSymbol.MARK.getValue()+t.getStartTime()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "end_time = "+SqlSymbol.MARK.getValue()+t.getEndTime()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "idea = "+SqlSymbol.MARK.getValue()+t.getIdea()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "idea_english = "+SqlSymbol.MARK.getValue()+t.getIdeaEnglish()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "profile = "+SqlSymbol.MARK.getValue()+t.getProfile()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "title = "+SqlSymbol.MARK.getValue()+t.getTitle()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "detail = "+SqlSymbol.MARK.getValue()+t.getDetail()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    /**
     * 事务
     * 1、在修改状态前，将所有状态修改为未启用
     * 2、将当前Id对应公司信息状态修改为启用
     * @param t
     * @return
     */
    @Override
    public int updateState(CompanyMessage t) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = jdbcMysql.getConnection();
            connection.setAutoCommit(false); //关闭事务自动提交
            statement = connection.createStatement();
//            修改所有公司信息状态为未启用
            sql = "update company_message set " +
                    "state = "+1+SqlSymbol.COMMA.getValue()+
                    "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue();
            int i = statement.executeUpdate(sql);
//            将当前Id所属公司信息修改为已启用
            sql = "update company_message set " +
                    "state = "+t.getState()+SqlSymbol.COMMA.getValue()+
                    "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                    " where id = "+t.getId();
            int i1 = statement.executeUpdate(sql);
            if (i < 1 || i1 != 1) {
                System.out.println("修改状态操作未成功！");
                connection.rollback();//数据回滚
                return 0;
            }
            connection.commit(); //事务提交
            return i1;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally { //关闭资源
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }
        return 0;
    }


    @Override
    public int delete(Integer id) {
        sql = "delete from company_message where id = "+id;
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

}
